package org.hxyjs.dao.impl;

import org.hxyjs.dao.FestivalDao;
import org.hxyjs.entity.Festival;
import org.hxyjs.mapper.FestivalMapper;
import org.hxyjs.page.PageInfo;
import org.hxyjs.utils.GenNumTools;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Repository("festivalDao")//一般实现类不会直接生成默认对象名 而是使用 接口的默认对象名
public class FestivalDaoImpl implements FestivalDao {
    @Resource //autowired 会装配能够直接扫描到的对象  resource spring容器运行的时候才找到需要的对象  注入
    private FestivalMapper festivalMapper;

    public List<Festival> getAllFestivals() throws Exception {
        return festivalMapper.selectByExample(null);
    }

    @Override
    public List<Festival> getFestivalByPage(PageInfo pageInfo) throws Exception {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("currPage", pageInfo.getCurrPage());
        map.put("pageSize", pageInfo.getPageSize());
        int itemCount = (int) festivalMapper.countByExample(null);
        pageInfo.setItemCount(itemCount);
        return festivalMapper.getFestivalByPage(map);
    }

    @Override
    public Festival selectFestivalBySno(String fno) throws Exception {
        return festivalMapper.selectByPrimaryKey(fno);
    }

    @Override
    public Integer deleteFestivalByFno(String fno) {
        return festivalMapper.deleteByPrimaryKey(fno);
    }

    @Override
    public Integer updateFestivalByFno(Festival festival) {
        return festivalMapper.updateByPrimaryKey(festival);
    }

    @Override
    public Integer addFestivalByFno(Festival festival) {
        festival.setFno(initId());
        return festivalMapper.insertSelective(festival);
    }

    public String initId() {
        String maxFno = festivalMapper.getMaxFno();
        return GenNumTools.initId("", 4, maxFno);
    }

    @Override
    public Festival getFestivalByFno(String fno) throws Exception {
        return festivalMapper.selectByPrimaryKey(fno);
    }
}